#!/bin/bash
# //===--------------------------- testit ---------------------------------===//
# //
# //                     The LLVM Compiler Infrastructure
# //
# // This file is distributed under the University of Illinois Open Source
# // License. See LICENSE.TXT for details.
# //
# //===--------------------------------------------------------------------===//

if [ -z $CXX ]
then
	CXX=clang++
fi

if [ -z "$OPTIONS" ]
then
	OPTIONS="-std=c++0x -stdlib=libc++"
fi

if [ -z "$QEMU" ]
then
    QEMU=qemu-system-arm
fi

case $TRIPLE in
  *-*-mingw* | *-*-cygwin* | *-*-win*)
    TEST_EXE_SUFFIX=.exe
    ;;
  *)
    TEST_EXE_SUFFIX=.out
    ;;
esac

FAIL=0
PASS=0
UNIMPLEMENTED=0
IMPLEMENTED_FAIL=0
IMPLEMENTED_PASS=0

function compile
{
  echo "  [COMPILE] $1"
  echo $CXX $OPTIONS $HEADER_INCLUDE $SOURCE_LIB $LIBS -o $1$TEST_EXE_SUFFIX $1 test_wrapper.cc
  $CXX $OPTIONS $HEADER_INCLUDE $SOURCE_LIB $LIBS -o $1$TEST_EXE_SUFFIX $1 test_wrapper.cc
}

function run
{
  echo "  [RUN    ] $1"
  case $TRIPLE in
    armv4-none-eabi)
      echo $QEMU -semihosting -M integratorcp -cpu arm1026 -kernel $1
      $QEMU -semihosting -M integratorcp -cpu arm1026 -kernel $1 | awk 'BEGIN { f=0; } /PASS/ { next } /FAIL/ { f=1; print; next } { print } END { exit f }'
      ;;
    thumbv4t-none-eabi)
      echo $QEMU -semihosting -M integratorcp -cpu arm926 -kernel $1
      $QEMU -semihosting -M integratorcp -cpu arm1026 -kernel $1 | awk 'BEGIN { f=0; } /PASS/ { next } /FAIL/ { f=1; print; next } { print } END { exit f }'
      ;;
    thumbv6m-none-eabi)
      echo $QEMU -semihosting -M integratorcp -cpu cortex-m3 -kernel $1
      $QEMU -semihosting -M integratorcp -cpu cortex-m3 -kernel $1 | awk 'BEGIN { f=0; } /PASS/ { next } /FAIL/ { f=1; print; next } { print } END { exit f }'
      ;;
    thumbv7t-none-eabi)
      echo $QEMU -semihosting -M integratorcp -cpu cortex-a8 -kernel $1
      $QEMU -semihosting -M integratorcp -cpu cortex-a8 -kernel $1 | awk 'BEGIN { f=0; } /PASS/ { next } /FAIL/ { f=1; print; next } { print } END { exit f }'
      ;;
    *)
      $1
      ;;
  esac
}

function afunc
{
	fail=0
	pass=0
	if (ls *.fail.cpp &> /dev/null)
	then
		for FILE in $(ls *.fail.cpp); do
			if compile $FILE &> /dev/null
			then
				rm $FILE$TEST_EXE_SUFFIX
				echo "  [FAIL   ] $FILE should not compile"
				let "fail+=1"
			else
				let "pass+=1"
			fi
		done
	fi

	if (ls *.cpp &> /dev/null)
	then
		for FILE in $(ls *.cpp); do
			if compile $FILE
			then
				if run $FILE$TEST_EXE_SUFFIX
				then
					let "pass+=1"
				else
					echo "  [FAIL   ] $FILE failed at run time"
					let "fail+=1"
				fi
			else
				echo "  [FAIL   ] $FILE failed to compile"
				let "fail+=1"
			fi
		done
	fi

	if [ $fail -gt 0 ]
	then
		echo "failed $fail tests in `pwd`"
		let "IMPLEMENTED_FAIL+=1"
	fi
	if [ $pass -gt 0 ]
	then
		echo "passed $pass tests in `pwd`"
		if [ $fail -eq 0 ]
		then
			let "IMPLEMENTED_PASS+=1"
		fi
	fi
	if [ $fail -eq 0 -a $pass -eq 0 ]
	then
		echo "not implemented:  `pwd`"
		let "UNIMPLEMENTED+=1"
	fi

	let "FAIL+=$fail"
	let "PASS+=$pass"

	for FILE in *
	do
		if [ -d "$FILE" ];
		then
			cd $FILE
			afunc
			cd ..
		fi
	done
}

afunc

echo "****************************************************"
echo "Results for `pwd`:"
echo "using `$CXX --version`"
echo "with $OPTIONS $HEADER_INCLUDE $SOURCE_LIB"
echo "----------------------------------------------------"
echo "sections without tests   : $UNIMPLEMENTED"
echo "sections with failures   : $IMPLEMENTED_FAIL"
echo "sections without failures: $IMPLEMENTED_PASS"
echo "                       +   ----"
echo "total number of sections : $(($UNIMPLEMENTED+$IMPLEMENTED_FAIL+$IMPLEMENTED_PASS))"
echo "----------------------------------------------------"
echo "number of tests failed   : $FAIL"
echo "number of tests passed   : $PASS"
echo "                       +   ----"
echo "total number of tests    : $(($FAIL+$PASS))"
echo "****************************************************"

exit $FAIL
